<!DOCTYPE html>
<html lang="en">

	<head>
		<meta charset="UTF-8">
		<title>Title</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<link rel="stylesheet" href="../../layui/css/layui.css" media="all">
		<style>
			.layui-laypage-limits {
				display: none !important;
			}
		</style>
	</head>

	<body>
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
			<legend style="text-align: center">FNST-在线笔试系统</legend>
		</fieldset>
		<div style="padding: 20px; background-color: #F2F2F2;">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12">
					<div class="layui-card">
						<div class="layui-card-header">笔试列表</div>
						<div action="" method="post">
							<div class="layui-card-body">
								<div class="layui-form-item">

									<button class="layui-btn "><a style="color:white" id="addEXAM"  > <i class="layui-icon">&#xe608;</i>新增笔试</a></button>
								</div>
							</div>
							<table id="demo" lay-filter="test"></table>
						</div>
					</div>
				</div>
			</div>
		</div>
		</div>
		<script type="text/html" id="barDemo">
			<a class="layui-btn  layui-btn-xs" lay-event="edit">修改</a>
			<a class="layui-btn  layui-btn-xs" lay-event="del">删除</a>
		</script>
		<script src="../../layui/layui.js"></script>
		<script src="../js/common.js"></script>
		<script type="text/javascript" src="../../js/jquery.min.js"></script>
		<script>
			layui.config({
				base: './../dist/'
			}).extend({
				notice: 'notice'
			});
			function formatDate(now) {
				var year = now.getFullYear();
				var month = now.getMonth() + 1;
				var date = now.getDate();
				var hour = now.getHours();
				var minute = now.getMinutes();
				var second = now.getSeconds();
				return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
			}
			layui.use(['notice','table'], function() {
				var table = layui.table;
				var notice = layui.notice; 				
				table.render({
					elem: '#demo',
					height: 312,
					url: host() + '/getAllQuestionsWithPage?token=' + localStorage.token,
					page: true,
					limit: 5,
					limits: [5, 10, 15, 20, 25, 30],
					cols: [
						[ //表头
							{
								field: 'id',
								title: '题目编号',
								width: 150
							}, {
								field: 'subject',
								title: '笔试题'
							},
							{
								field: '',
								title: '创建时间',
								width: 250,
								templet: function(d) {
									var d = new Date(d.createTime);
									return formatDate(d);
								}
							}, {
								fixed: 'right',
								title: '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;操作',
								toolbar: '#barDemo',
								width: 180
							}
						]
					]
				});
				table.on('tool(test)', function(obj) {
					var data = obj.data;
					if(obj.event === 'del') {
						layer.confirm('真的删除数据吗',
                            {
                                offset:"220px"
                            },
							function(index) {
							$.ajax({
								type: 'get',
								url: host() + '/deleteQuestion?token=' + localStorage.token,
								data: {
									questionId: data.id
								},
								dataType: 'json',
								async: false,
								success: function(msg) {
                                    if(msg.code!=200){
                                        notice.warning(msg.msg);
                                        layer.close(index);
                                        return;
                                    }
									notice.success("删除笔试题成功");
									obj.del();
									layer.close(index);
								},
								error: function(msg) {
									notice.error("删除笔试题失败");
                                    layer.close(index);
								}
							})
						});
					}
					if(obj.event === 'edit') {
						localStorage.setItem("questionId", data.id);
						localStorage.setItem("subject", data.subject);
						window.open("examUpdate.html", "iframe");
					}
				});

			});
			$("#addEXAM").click(function() {
				window.open("examAdd.html", "iframe")
			})
		</script>
	</body>

</html>